package com.netease.musicweekly.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

import com.netease.musicweekly.entity.Article;
import com.netease.musicweekly.entity.Category;

public class CategoryParser extends BaseXMLParser {

	public Category getCategoryFromXML(String xml) {
		if(null == xml){
			return null;
		}
		final Category category = new Category();
		RootElement root = new RootElement("category");
		Element arts = root.getChild("articles");
		Element arti = arts.getChild("article");
		final List<Article> articles = new ArrayList<Article>();
		final Article art = new Article();
		root.getChild("id").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String id) {
						category.id = id;
					}
				});
		root.getChild("name").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String name) {
						category.name = name;
					}
				});
		root.getChild("articles").setEndElementListener(
				new EndElementListener() {
					public void end() {
						category.articles = articles;
					}
				});
		arti.getChild("title").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String title) {
						art.title = title;
					}
				});
		arti.getChild("body").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						art.body = body;
					}
				});
		arti.getChild("source").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String source) {
						art.source = source;
					}
				});
		arti.getChild("date").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String date) {
						art.date = date;
					}
				});
		arts.getChild("article").setEndElementListener(
				new EndElementListener() {
					public void end() {
						articles.add(art.copy());
					}
				});
		try {
			Xml.parse(xml, root.getContentHandler());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return category;
	}

	public Category getCategoryFromInputStream(InputStream is)
			throws SAXException, IOException {
		final Category category = new Category();
		RootElement root = new RootElement("category");
		Element arts = root.getChild("articles");
		final List<Article> articles = new ArrayList<Article>();
		final Article art = new Article();
		root.getChild("id").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String id) {
						category.id = id;
					}
				});
		root.getChild("name").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String name) {
						category.name = name;
					}
				});
		root.getChild("articles").setEndElementListener(
				new EndElementListener() {
					public void end() {
						category.articles = articles;
					}
				});
		arts.getChild("title").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String title) {
						art.title = title;
					}
				});
		arts.getChild("body").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						art.body = body;
					}
				});
		arts.getChild("source").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String source) {
						art.source = source;
					}
				});
		arts.getChild("date").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String date) {
						art.date = date;
					}
				});
		arts.getChild("article").setEndElementListener(
				new EndElementListener() {
					public void end() {
						articles.add(art.copy());
					}
				});
		Xml.parse(is, Xml.Encoding.UTF_8, root.getContentHandler());
		return category;
	}
}
